#include "Game.h"

void Game::init(Handle* a_handle)
{
	m_running = true;
	m_core.init("Game",CL_BLACK,1280,768,true,a_handle);

	m_system.queueCartridge(new GameMenuState(&m_core));
	m_system.queueCartridge(new GamePlayState(&m_core,false));
	m_system.queueCartridge(new GameWinState(&m_core));
	m_system.queueCartridge(new GameLoseState(&m_core));
	m_system.queueCartridge(new GameOptionsState(&m_core));
	m_system.queueCartridge(new GameCreditsState(&m_core));
	m_system.powerOffId(GAMEQUIT);
	m_system.powerOnSystem();

	m_debug.init(&m_core,CL_RED);
	m_debug.watch(m_system.getCartridgeName());
	m_debug.watch("FPS: ",m_core.ptrFPS());

	m_text.init(&m_core,CL_RED);
	m_text.watch("Play Controls");
	m_text.watch("-----------------------");
	m_text.watch("W,A,S,D Keys - Movement");
}

void Game::release()
{
	m_debug.release();
	m_system.powerOffSystem();
	m_core.release();
}

void Game::input()
{
	m_core.acquire();
	m_system.input();
}

void Game::update()
{
	m_core.update();
	m_system.update();
}

void Game::draw()
{
	m_core.drawBegin();

	m_system.draw();
	m_debug.drawTLD();
	m_text.drawBLD();

	m_core.drawEnd();
}

void Game::preLoop()
{
	m_system.preLoop();
}

void Game::postLoop()
{
	m_system.postLoop();
	m_system.checkCartridge();
}

bool Game::running(int& a_error)
{
	if(!m_running || m_core.exit() || !m_system.isPowered())
	{
		a_error = m_core.exitMessage();
		return false;
	}

	return true;
}